package com.yanboo.modules.mnt.rest;

import com.yanboo.common.annotation.CustomController;
import com.yanboo.logging.annotation.Log;
import com.yanboo.modules.mnt.domain.Processor;
import com.yanboo.modules.mnt.service.ProcessorService;
import com.yanboo.modules.mnt.service.dto.ProcessorQueryCriteria;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.io.IOException;
import jakarta.servlet.http.HttpServletResponse;
import com.yanboo.common.utils.PageResult;
import com.yanboo.modules.mnt.service.dto.ProcessorDto;

/**
 * @author fdq
 * 单机通用版进程管理，作为DeployController的补充，
 * DeployController定位仅适用带端口号的程序
 * 如果需要远程部署请使用DeployController
 * @date 2024-03-27
 **/
@CustomController
@RequiredArgsConstructor
@Tag(name = "processor管理")
@RequestMapping("/api/processor")
public class ProcessorController {

    private final ProcessorService processorService;

    @Log("导出数据")
    @Operation(summary = "导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('processor:list')")
    public void exportProcessor(HttpServletResponse response, ProcessorQueryCriteria criteria) throws IOException {
        processorService.download(processorService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询processor")
    @Operation(summary = "查询processor")
    @PreAuthorize("@el.check('processor:list')")
    public ResponseEntity<PageResult<ProcessorDto>> queryProcessor(ProcessorQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(processorService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增processor")
    @Operation(summary = "新增processor")
    @PreAuthorize("@el.check('processor:add')")
    public ResponseEntity<Object> createProcessor(@Validated @RequestBody Processor resources) throws IOException, InterruptedException {
        processorService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改processor")
    @Operation(summary = "修改processor")
    @PreAuthorize("@el.check('processor:edit')")
    public ResponseEntity<Object> updateProcessor(@Validated @RequestBody ProcessorDto resources){
        processorService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除processor")
    @Operation(summary = "删除processor")
    @PreAuthorize("@el.check('processor:del')")
    public ResponseEntity<Object> deleteProcessor(@RequestBody Long[] ids) {
        processorService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @PostMapping("/exec")
    @Log("执行processor")
    @Operation(summary = "执行processor")
    @PreAuthorize("@el.check('processor:edit')")
    public ResponseEntity<Object> execProcessor(@RequestBody ProcessorDto resources) throws IOException, InterruptedException {
        processorService.execAsync(resources);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @PostMapping("/destroy")
    @Log("销毁processor")
    @Operation(summary = "销毁processor")
    @PreAuthorize("@el.check('processor:edit')")
    public ResponseEntity<Object> destroyProcessor(@RequestBody ProcessorDto resources) throws IOException, InterruptedException {
        processorService.destroy(resources);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @PostMapping("/destroyForcibly")
    @Log("强制销毁processor")
    @Operation(summary = "强制销毁processor")
    @PreAuthorize("@el.check('processor:edit')")
    public ResponseEntity<Object> destroyForcibly(@RequestBody ProcessorDto resources) throws IOException, InterruptedException {
        processorService.destroyForcibly(resources);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}
